﻿using System;
using System.Collections.Generic;
using System.Text;
using GroupChat2013.Entity.Protocol;
using GroupChat2013.Common;
using GroupChat2013.Interface;
using GroupChat2013.Logic;
using GroupChat2013.Logic.Interface;

namespace GroupChat2013.Main.Components
{
    /// <summary>
    /// 群相关通知
    /// </summary>
    public class GroupChangeReceiveProcess : IReceiveProcess
    {
        /// <summary>
        /// 群详情变更消息处理标志
        /// </summary>
        private string detailKey;

        /// <summary>
        /// 群成员列表更新通知处理标志
        /// </summary>
        private string memberKey;

        private string groupId = "groupId";

        /// <summary>
        /// 新的群公告通知处理标志
        /// </summary>
        private string noticeKey;

        public GroupChangeReceiveProcess()
        {
            detailKey = ConstClass.GETCURRENTGROUPINFO;
            memberKey = ConstClass.GETGROUPMEMBERLIST;
            noticeKey = ConstClass.GETLATESTGROUPNOTICELIST;
        }

        public bool Process(TransferMsgInfo message)
        {
            if (message.action.Equals(detailKey))
            {
                GroupDetailProcess(message);
                return true;
            }

            if (message.action.Equals(memberKey))
            {
                GroupMemberProcess(message);
                return true;
            }

            if (message.action.Equals(noticeKey))
            {
                GroupNoticeProcess(message);
                return true;
            }

            return false;
        }

        /// <summary>
        /// 群详情变更
        /// </summary>
        private void GroupDetailProcess(TransferMsgInfo message)
        {
            if (message.data.ContainsKey(groupId))
            {
                string id = message.data[groupId];
                IChat chat = EngineContext.Current.ChatManager[id];
                if (chat != null && chat is IGroupChat)
                {
                    (chat as IGroupChat).UpdateDetail();
                }
            }
        }

        /// <summary>
        /// 群成员列表
        /// </summary>
        private void GroupMemberProcess(TransferMsgInfo message)
        {
            string id = message.data[groupId];
            IChat chat = EngineContext.Current.ChatManager[id];
            if (chat != null && chat is IGroupChat)
            {
                (chat as IGroupChat).UpdateUserList();
            }
        }

        /// <summary>
        /// 新的群公告
        /// </summary>
        private void GroupNoticeProcess(TransferMsgInfo message)
        {
            string id = message.data[groupId];
            IChat chat = EngineContext.Current.ChatManager[id];
            if (chat != null && chat is IGroupChat)
            {
                (chat as IGroupChat).UpdateNotice();
            }
        }
    }
}
